package com.link.base.base.questionnairesurvey.service;

import com.link.base.user.model.CoreUser;
import com.link.base.base.questionnairesurvey.dao.mybatis.mapper.AnswerMapper;
import com.link.base.base.questionnairesurvey.dao.mybatis.mapper.TabMapper;
import com.link.base.base.questionnairesurvey.dao.mybatis.mapper.TopicMapper;
import com.link.base.base.questionnairesurvey.model.Answer;
import com.link.base.base.questionnairesurvey.model.Survey;
import com.link.base.base.questionnairesurvey.model.Tab;
import com.link.base.base.questionnairesurvey.model.Topic;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.query.Sorter;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 刘鲁
 * @date 2018-07-04
 * @comments 作答用户答案
 */
@Service
public class AnswerServiceImpl extends BasicServiceImpl<Answer> implements AnswerService {

    @Resource
    private AnswerMapper answerMapper;
    @Resource
    private TabMapper tabMapper;
    @Resource
    private TopicMapper topicMapper;

    @Override
    public BasicMapper<Answer> getBasicMapper() {
        return answerMapper;
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void batchInsert(List<Answer> list) throws Exception {
        CoreUser user = UserUtil.getUser();
        Answer answer = list.get(0);
        if (answer.getEmpId() == null) {
            answer.setEmpId(user.getId());
        }
        //清楚之前选的答案
        answerMapper.deleteByTopic(answer);
        for (int i = 0; !list.isEmpty() && i < list.size(); i++) {
            Answer item = list.get(i);
            if (item.getEmpId() == null) {
                item.setEmpId(user.getId());
            }
            insert(item);
        }
    }

    /**
     * 题目结果统计
     *
     * @param topic
     * @return
     */
    @Override
    public Map<String, Object> queryAnsByTopicId(Topic topic) throws Exception {
        Map<String, Object> res = new LinkedHashMap<>();

        Tab queryTab = new Tab();
        queryTab.setTopicID(topic.getId().toString());
        queryTab.setLoginCorpId(topic.getLoginCorpId());
        queryTab.setPageFlag(false);
        QueryParams qps = new QueryParams();
        Sorter s = new Sorter("sequence", Sorter.DIR_ASC);
        List<Sorter> list = new ArrayList<>();
        list.add(s);
        qps.setSorters(list);
        queryTab = (Tab) BasicModel.transformClass(queryTab, qps);

        List<Tab> tabList = tabMapper.queryByExamplePage(queryTab);
        String arrayChoice = "ArrayChoice";
        String arrayRadio = "ArrayRadio";
        if (arrayChoice.equals(topic.getTypetopic()) || arrayRadio.equals(topic.getTypetopic())) {
            for (Tab tab : tabList) {
                if (StringUtils.equals(tab.getTypeTab(), "Group")) {
                    Answer query = new Answer();
                    query.setLoginCorpId(topic.getLoginCorpId());
                    query.setGroupID(tab.getId());
                    query.setPageFlag(false);
                    Map<String, Integer> tmpMap = new HashMap<>();
                    for (Tab t : tabList) {
                        if (StringUtils.equals(t.getTypeTab(), "Tab")) {
                            query.setTabId(t.getId());
                            List<Answer> answerList = answerMapper.queryByExamplePage(query);
                            tmpMap.put(t.getTabcontent(), answerList.size());
                        }
                    }
                    res.put(tab.getTabcontent(), tmpMap);
                }
            }
        } else {
            for (Tab tab : tabList) {
                Answer query = new Answer();
                query.setLoginCorpId(topic.getLoginCorpId());
                query.setTabId(tab.getId());
                query.setPageFlag(false);
                List<Answer> answerList = answerMapper.queryByExamplePage(query);
                res.put(tab.getTabcontent(), answerList.size());
            }
        }


        for (Tab tab : tabList) {
            Answer query = new Answer();
            query.setLoginCorpId(topic.getLoginCorpId());
        }

        Answer query = new Answer();
        query.setTopicID(topic.getId());
        query.setLoginCorpId(topic.getLoginCorpId());

        List<Answer> answerList = answerMapper.queryByExamplePage(query);

        //插入回答总数
        res.put("", answerList.size());
        return res;
    }

    @Override
    public Map<String, Map<String, Object>> queryAnsBySurveyId(Survey survey) throws Exception {
        Map<String, Map<String, Object>> res = new LinkedHashMap<>();
        Topic query = new Topic();
        query.setLoginCorpId(survey.getLoginCorpId());
        query.setSurveyId(survey.getId());
        query.setPageFlag(false);

        QueryParams qps = new QueryParams();
        Sorter s = new Sorter("sequence", Sorter.DIR_ASC);
        List<Sorter> list = new ArrayList<>();
        list.add(s);
        qps.setSorters(list);
        Topic temp = (Topic) BasicModel.transformClass(query, qps);

        List<Topic> topics = topicMapper.queryByExamplePage(temp);

        int i = 1;
        for (Topic t : topics) {
            if (StringUtils.equals("GapFilling", t.getTypetopic())) {
                continue;
            }

            res.put((i++) + " . " + t.getTopicDescription(), queryAnsByTopicId(t));

        }

        return res;
    }

}

